package com.eeepay.eeepay_v2.activity;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.adapter.IntoPagerAdapter;
import com.eeepay.eeepay_v2.fragment.LimitFragment;
import com.eeepay.eeepay_v2.fragment.OtherInfoFragment;
import com.eeepay.eeepay_v2.fragment.RateFragment;
import com.eeepay.eeepay_v2.model.UserInfo;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.GrpcUtils;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.view.LabelEditText;
import com.eeepay.v2_library.view.TitleBar;

import java.util.ArrayList;
import java.util.List;

import cn.eeepay.api.grpc.nano.MerAddModular;
import cn.eeepay.api.grpc.nano.MerAddServiceGrpc;
import io.grpc.ManagedChannel;

/**
 * Created by Ching on 2016/6/29.
 * 资料详情界面
 */
public class DataDetailsActivity extends ABBaseActivity implements TitleBar.LeftBtnOnClickListener {

//    private PagerSlidingTabStrip tabStrip;
    private TabLayout tabStrip;
    private ViewPager viewPager;
    private LabelEditText phoneLet, snoLet;
    private TitleBar titleBar;

    private IntoPagerAdapter adapter;
    private List<Fragment> list = new ArrayList<>();
    private RateFragment rateFragment;
    private LimitFragment limitFragment;
    private OtherInfoFragment infoFragment;
    private String bp_id, mer_no;
   private MerAddModular.MerServiceRate[] rateList;
   private MerAddModular.MerServiceQuota[] quotaList;
    private MerAddModular.MerRequireItem[] photo;
   private MerAddModular.MerRequireItem[] other;
    private MerAddModular.MerchantInfo merInfo;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_data_details;
    }

    @Override
    protected void initWidget() {
        titleBar = getViewById(R.id.tb_data_details);
        tabStrip = getViewById(R.id.psts_title);
        viewPager = getViewById(R.id.vp_into_query);
        phoneLet = getViewById(R.id.let_into_query_phone);
        snoLet = getViewById(R.id.let_into_query_sno);
    }

    @Override
    protected void eventOnClick() {
        titleBar.setLeftOnClickListener(this);
        Intent intent = getIntent();
        if (intent != null) {
            bp_id = intent.getStringExtra(Constant.KEY.BP_ID);
            mer_no = intent.getStringExtra(Constant.KEY.MER_NO);
            adapter = new IntoPagerAdapter(getSupportFragmentManager());
            Log.d("DataD", "bp_id -->" + bp_id + "|  mer_no -->" + mer_no);
            phoneLet.setEditContent(intent.getStringExtra(Constant.KEY.MER_PHONE));
            snoLet.setEditContent(intent.getStringExtra(Constant.KEY.SNO));
        }
        sendHttpRequest(0);
    }

    @Override
    protected void sendHttpRequest(int taskId) {
        showProgressDialog(getResources().getString(R.string.request_data));
        GrpcUtils.getInstance().doGrpcTask(Constant.URL.URL, Constant.URL.PORT, taskId, new GrpcUtils.AsyncTaskListener() {
            @Override
            public Object doInBack(ManagedChannel channel, int taskId) {
                MerAddServiceGrpc.MerAddServiceBlockingStub stub = MerAddServiceGrpc.newBlockingStub(channel);
                MerAddModular.QueryMerInfoReq request = new MerAddModular.QueryMerInfoReq();
                UserInfo userInfo = UserInfo.getInstance();
                request.bpId = bp_id;
                request.merchantNo = mer_no;
                request.parentNode = userInfo.getAgentNode();
                request.oneAgentNo = userInfo.getOneAgentNo();
                return stub.queryMerItemDetails(request);
            }

            @Override
            public void updateUI(Object result, int taskId) {
                dismissProgressDialog();
                MerAddModular.MerItemDetailsList response = (MerAddModular.MerItemDetailsList) result;
                if (result != null) {
                    if (response.resMsg.status) {
                         rateList = response.merServiceRateList;
                         quotaList = response.merServiceQuotaList;
                         photo = response.photoRequireItemList;
                         other = response.prayerRequireItemList;
                         merInfo = response.merInfo;
                        for (MerAddModular.MerServiceRate rate : rateList) {
                            Log.d("dataDetails", "rate　＞＞＞　id:" + rate.id + "| rate:" + rate.rate);
                        }
                        for (MerAddModular.MerServiceQuota quota : quotaList) {
                            Log.d("dataDetails", "quota　＞＞＞ id:" + quota.id +
                                    "| singleCountAmount:" + quota.singleCountAmount);
                        }
                        for (MerAddModular.MerRequireItem pic : photo) {
                            Log.d("dataDetails", "photo　＞＞＞ id:" + pic.id + "| rate:" + pic.content);
                        }
                        rateFragment = new RateFragment();
                        limitFragment = new LimitFragment();
                        infoFragment = new OtherInfoFragment();
                        list.add(rateFragment);
                        list.add(limitFragment);
                        list.add(infoFragment);

                        adapter.setList(list);
                        viewPager.setAdapter(adapter);
                        viewPager.setOffscreenPageLimit(2);
                        //add by zhuangzeqin 2017年9月4日10:31:15 减少导入PagerSlidingTabStrip 库的大小；用目前现在已经可以实现的功能即可
                        tabStrip.setTabGravity(TabLayout.GRAVITY_FILL);
                        tabStrip.setTabMode(TabLayout.MODE_FIXED);
                        tabStrip.setupWithViewPager(viewPager);
//                        tabStrip.setViewPager(viewPager);
                    } else {
                        showToast(response.resMsg.msg);
                    }
                } else {
                    showToast("返回数据为空");
                }
            }
        });
    }

    @Override
    public void onLeftClick(View view) {
        this.finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { //按下的如果是BACK，同时没有重复
            onLeftClick(null);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public MerAddModular.MerServiceRate[] getRateList() {
        return rateList;
    }

    public MerAddModular.MerServiceQuota[] getQuotaList() {
        return quotaList;
    }

    public MerAddModular.MerRequireItem[] getPhoto() {
        return photo;
    }

    public MerAddModular.MerRequireItem[] getOther() {
        return other;
    }

    public MerAddModular.MerchantInfo getMerInfo() {
        return merInfo;
    }
}
